﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using MxTol.Models;
using MxTol.UI.Helpers;
using MxTol.Managers;

namespace MxTol.UI.Controllers
{
    /// <summary>
    /// 财经法规案例分析选择题控制器
    /// </summary>
    public class FrAnalysisChoiceManagementController : Controller
    {
        private const string MIME_TYPE = "application/json;charset=utf-8;";

        #region View

        // 索引页面
        public ActionResult GetAllChoiceView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                string id = Request["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    return View(new Guid(id));
                }
                else
                {
                    return Content("获取该案例分析题的选择题列表失败！");
                }
            }
        }

        // 添加案例分析选择题
        public ActionResult AddChoiceView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                string id = Request["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    return View(new Guid(id));
                }
                else
                {
                    return Content("获取该案例分析题GUID失败！");
                }
            }
        }

        // 修改案例分析选择题
        public ActionResult UpdateChoiceView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                string id = Request["id"];
                FrAnalysisChoice choice = null;
                try
                {
                    choice = FrAnalysisChoice.GetChoiceByID(new Guid(id));
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLogAsync("修改财经法规案例分析选择题信息Action失败", ex);
                }

                if (choice != null)
                {
                    choice.Title = MethodHelper.ToOneLine(choice.Title);
                    return View(choice);
                }
                else
                {
                    return Content("获取案例分析选择题信息失败");
                }
            }
        }

        #endregion

        #region 功能

        // 以Json分页格式返回特定案例分析的所有选择题
        public ActionResult GetFrChoiceJsonByID()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string id = Request["id"];
                string currentPage = Request["page"];
                string pageSize = Request["rows"];

                string sql = "select * from Fr_Analysis_Choice where descripe_id = '{0}' order by add_time desc;";
                sql = string.Format(sql, id);

                json = FrAnalysisChoiceManager.GetAllChoice(int.Parse(currentPage), int.Parse(pageSize), sql);
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("获取财经法规案例分析选择题题库Json格式数据错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        // 添加案例分析选择题
        public ActionResult AddChoice()
        {
            // 0：异常失败  1：成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                string title = Request["title"];
                string a = Request["a"];
                string b = Request["b"];
                string c = Request["c"];
                string d = Request["d"];
                string key = Request["key"];
                string analysis = Request["analysis"];
                if (key.Trim().EndsWith("、")) { key = key.Trim().Substring(0, key.Length - 1); }

                if (Session["account"] != null)
                {
                    FrAnalysisChoice choice = new FrAnalysisChoice();

                    choice.DescripeID = new Guid(id);
                    choice.Title = title;
                    choice.A = a;
                    choice.B = b;
                    choice.C = c;
                    choice.D = d;
                    choice.Answer = key;
                    choice.Analysis = analysis;
                    choice.AddPerson = AdminManager.GetAdminName(Session["account"].ToString());
                    choice.AddTime = DateTime.Now;

                    choice.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("添加财经法规案例分析选择题失败", ex);
            }

            return Content(flag.ToString(), MIME_TYPE);
        }

        // 删除案例分析选择题
        public ActionResult DeleteChoice()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                FrAnalysisChoice choice = FrAnalysisChoice.GetChoiceByID(new Guid(id));
                if (choice != null)
                {
                    choice.Delete();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("删除财经法规案例分析选择题失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 修改案例分析选择题
        public ActionResult UpdateChoice()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                if (Session["account"] == null)
                {
                    string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                    return Content(script);
                }

                string id = Request["id"];
                FrAnalysisChoice choice = FrAnalysisChoice.GetChoiceByID(new Guid(id));
                if (choice != null)
                {
                    string title = Request["title"];
                    string a = Request["a"];
                    string b = Request["b"];
                    string c = Request["c"];
                    string d = Request["d"];
                    string key = Request["key"];
                    string analysis = Request["analysis"];
                    if (key.Trim().EndsWith("、")) { key = key.Trim().Substring(0, key.Length - 1); }

                    choice.Title = title;
                    choice.A = a;
                    choice.B = b;
                    choice.C = c;
                    choice.D = d;
                    choice.Answer = key;
                    choice.Analysis = analysis;

                    choice.Update();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("修改财经法规案例分析选择题失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        #endregion

    }
}
